package net.mingsoft.config;

import java.time.Duration;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration.DefaultJedisClientConfigurationBuilder;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;

import redis.clients.jedis.JedisPoolConfig;

/**
 * Redis配置类
 * Created by macro on 2020/3/2.
 */
@EnableCaching
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
	
	
	public RedisConnectionFactory connectionFactory(String host, int port, String password, int maxIdle,
            int maxTotal, int minIdle, long maxWaitMillis, int index,long connTimeout) {
		RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(host, port);
		config.setPassword(RedisPassword.of(password));
		config.setDatabase(index);
		JedisPoolConfig poolConfig = new JedisPoolConfig();
		poolConfig.setMaxIdle(maxIdle);
		poolConfig.setMinIdle(minIdle);
		poolConfig.setMaxWaitMillis(maxWaitMillis);
		poolConfig.setMaxTotal(maxTotal);
		DefaultJedisClientConfigurationBuilder jedisClientConfiguration = (DefaultJedisClientConfigurationBuilder)JedisClientConfiguration.builder();
		jedisClientConfiguration.poolConfig(poolConfig);
		jedisClientConfiguration.connectTimeout(Duration.ofMillis(connTimeout));
		JedisClientConfiguration clientConfig = jedisClientConfiguration.build();
		JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(config,clientConfig);
		jedisConnectionFactory.afterPropertiesSet();
		return jedisConnectionFactory;
	}
	
	@Bean(name = "cacheFactory")
	@Primary
	public RedisConnectionFactory getCacheConnectionFactory(
			@Value("${spring.redis.host}") String host, 
			@Value("${spring.redis.port}") int port, 
			@Value("${spring.redis.password}")String password, 
			@Value("${spring.redis.pool.max-idle}")int maxIdle,
			@Value("${spring.redis.pool.min-idle}")int minIdle, 
			@Value("${spring.redis.pool.max-total}") int maxTotal, 
			@Value("${spring.redis.pool.max-wait}")long maxWaitMillis, 
			@Value("${spring.redis.database}")int index,
			@Value("${spring.redis.timeout}")long connTimeout) {
		return connectionFactory(host, port, password, maxIdle, maxTotal, minIdle, maxWaitMillis, index, connTimeout);
	}
	
	
    @Bean("cacheRedisTemplate")
    public RedisTemplate<String, Object> cacheRedisTemplate(@Qualifier("cacheFactory") RedisConnectionFactory redisConnectionFactory) {
        RedisSerializer<Object> serializer = redisSerializer();
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(serializer);
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(serializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    @Bean
    public RedisSerializer<Object> redisSerializer() {
        //创建JSON序列化器
        Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        serializer.setObjectMapper(objectMapper);
        return serializer;
    }
}
